package com.xiang.publicdemo.module2.service.impl;

import com.xiang.publicdemo.common.utils.StringUtils;
import com.xiang.publicdemo.module2.service.ITestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class TestServiceImpl implements ITestService {

    private boolean produceFlag = true;
    private boolean consumFlag = true;
    private ArrayBlockingQueue<String> que = new ArrayBlockingQueue<String>(1000);

    @Override
    public void test(String aa) {
        for (int i = 0, j = 10; i < j; i++) {
            Thread thr = new Thread() {
                public void run() {
                    produce();
                }
            };
            thr.setDaemon(true);
            thr.setName("producer-" + i);
            thr.start();
        }

        for (int i = 0, j = 10; i < j; i++) {
            Thread consumer = new Thread() {
                public void run() {
                    consum();
                }
            };
            consumer.setDaemon(true);
            consumer.setName("consumer-"+i);
            consumer.start();
        }
    }

    //生产方法
    private void produce() {
        Long i = 0l;
        StringBuilder sb = new StringBuilder();
        while (produceFlag) {
            String temp = i.toString();
            boolean result = que.offer(temp);
            if (!result) {
                try {
                    Thread.sleep(1000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                sb.setLength(0);
                sb.append(Thread.currentThread().getName()).append("-生成:").append(i.toString());
                System.out.println(sb.toString());
            }
            i++;
        }
    }

    //消费方法
    private void consum() {
        StringBuilder sb = new StringBuilder();
        while (consumFlag) {
            try {
                String message = que.poll(20, TimeUnit.SECONDS);
                if (StringUtils.isEmpty(message)) {
                    Thread.sleep(500l);
                    continue;
                }
                List<String> messages = new LinkedList<String>();
                messages.add(message);
                que.drainTo(messages, 10);
                for (String msg : messages) {
                    sb.setLength(0);
                    sb.append(Thread.currentThread().getName()).append("-消费:").append(msg);
                    System.out.println(sb.toString());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        ITestService testService = new TestServiceImpl();
        testService.test("aaa");
        Thread.sleep(1000l);
    }
}